package com.base.andlang2.andlangrn

import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.view.KeyEvent
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.base.andlang2.andlangrn.databinding.ActivityRnwebBinding
import com.base.andlangcore.AndLangBaseApp
import com.base.andlangcore.mvvm.AndLangActivity
import com.base.andlangcore.mvvm.AndLangModel
import com.base.andlangrouter.RouteUrl
import com.facebook.hermes.reactexecutor.HermesExecutorFactory
import com.facebook.react.ReactInstanceManager
import com.facebook.react.common.LifecycleState
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
import com.facebook.react.shell.MainReactPackage
import com.facebook.soloader.SoLoader

@Route(path = RouteUrl.RN_MAIN_WEB)
class RNWebActivity : AndLangActivity<ActivityRnwebBinding,AndLangModel>(),
    DefaultHardwareBackBtnHandler {

    @JvmField
    @Autowired
    var bundleAssetName: String? = null

    @JvmField
    @Autowired
    var jSMainModulePath: String? = null

    @JvmField
    @Autowired
    var moduleName: String? = null

    private val OVERLAY_PERMISSION_REQ_CODE = 1 // 任写一个值
    private lateinit var mReactInstanceManager: ReactInstanceManager

    override fun getViewBinding(): ActivityRnwebBinding {
        return ActivityRnwebBinding.inflate(layoutInflater)
    }

    override fun getVM(): AndLangModel {
        return ViewModelProvider(this)[AndLangModel::class.java]
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        SoLoader.init(this, false)
        mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(AndLangBaseApp.application)
            .setCurrentActivity(this)
            .setBundleAssetName(bundleAssetName)
            .setJSMainModulePath(jSMainModulePath)
            .addPackage(MainReactPackage())
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .setJavaScriptExecutorFactory(HermesExecutorFactory()) //关键方法
            .build()
        vb.rnView.startReactApplication(mReactInstanceManager, moduleName, null)
    }

    override fun initData() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    // SYSTEM_ALERT_WINDOW permission not granted
                }
            }
        }
        mReactInstanceManager?.onActivityResult(this, requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun invokeDefaultOnBackPressed() {
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        mReactInstanceManager.onHostPause(this)
    }

    override fun onResume() {
        super.onResume()
        mReactInstanceManager.onHostResume(this, this)
    }

    override fun onDestroy() {
        super.onDestroy()
        mReactInstanceManager.onHostDestroy(this)
        vb.rnView.unmountReactApplication()
    }

    override fun onBackPressed() {
        mReactInstanceManager?.onBackPressed()
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager.showDevOptionsDialog()
            return true
        }
        return super.onKeyUp(keyCode, event)
    }
}